package com.lib.basex.widget.border;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.lib.basex.R;
import com.lib.basex.utils.LUtils;

public class LClearEditText extends LEditView implements View.OnFocusChangeListener, TextWatcher {

    /**
     * 删除按钮的引用
     */
    private Drawable mClearDrawable;
    /**
     * 控件是否有焦点
     */
    private boolean hasFocus;

    /**
     * 标记
     */
    private static final int DRAWABLE_LEFT = 0; //左边图标
    private static final int DRAWABLE_TOP = 1;
    private static final int DRAWABLE_RIGHT = 2;
    private static final int DRAWABLE_BOTTOM = 3;

    //清除功能是否可用
    private boolean disableClean;

    private OnClearClickListener mOnClearClickListener;
    private OnTextChangedListener onTextChangedListener;

    public LClearEditText(Context context) {
        super(context);
    }

    public LClearEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void init(AttributeSet attrs) {
        super.init(attrs);
        disableClean = true;
        int i = LUtils.dip2px(20);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
        int paddingRight = getPaddingRight() == 0 ? LUtils.dip2px(8) : getPaddingRight();
        setPadding(getPaddingLeft(), getPaddingTop(), paddingRight, getPaddingBottom());
        setDelDrawable(R.drawable.l_ic_edit_del, i, i);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        updateDrawable(hasFocus);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFocus = hasFocus;
        updateDrawable(hasFocus);
    }

    public void setDelDrawable(int drawableId, int width, int height) {
        mClearDrawable = ActivityCompat.getDrawable(getContext(), drawableId);
        mClearDrawable.setBounds(0, 0, width, height);
        updateDrawable(hasFocus);

    }

    // 更新删除图片状态: 当内容不为空，而且获得焦点，才显示右侧删除按钮
    private void updateDrawable(boolean hasFocus) {
        if (length() > 0 && hasFocus && disableClean) {
            setCompoundDrawables(getCompoundDrawables()[DRAWABLE_LEFT], getCompoundDrawables()[DRAWABLE_TOP], mClearDrawable, getCompoundDrawables()[DRAWABLE_BOTTOM]);
        } else {
            setCompoundDrawables(getCompoundDrawables()[DRAWABLE_LEFT], getCompoundDrawables()[DRAWABLE_TOP], null, getCompoundDrawables()[DRAWABLE_BOTTOM]);
        }
    }

    @Override
    protected void finalize() throws Throwable {
        mClearDrawable = null;
        super.finalize();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //可以获得上下左右四个drawable，右侧排第二。图标没有设置则为空。
        Drawable rightIcon = getCompoundDrawables()[DRAWABLE_RIGHT];
        if (rightIcon != null && event.getAction() == MotionEvent.ACTION_UP) {
            //检查点击的位置是否是右侧的删除图标
            //注意，使用getRwwX()是获取相对屏幕的位置，getX()可能获取相对父组件的位置
            int leftEdgeOfRightDrawable = getRight() - getPaddingRight()
                    - rightIcon.getBounds().width();
            if (event.getRawX() >= leftEdgeOfRightDrawable) {
                setText("");
                if (mOnClearClickListener != null) {
                    mOnClearClickListener.onClick();
                }
            }
        }

        return super.onTouchEvent(event);
    }

    //右边按钮的点击事件
    public interface OnClearClickListener {
        void onClick();
    }

    public void setOnTextChangedListener(OnTextChangedListener onTextChangedListener) {
        this.onTextChangedListener = onTextChangedListener;
    }

    //设置监听
    public void setOnClearClickListener(OnClearClickListener mOnClearClickListener) {
        this.mOnClearClickListener = mOnClearClickListener;
    }

    public interface OnTextChangedListener{
        void onTextChangedListener(Editable editable);
    }
}
